package com.lxitedu.chain.processor;

import java.util.List;

import lombok.SneakyThrows;

import com.lxitedu.utils.Context;
import com.lxitedu.utils.LxitException;
import com.lxitedu.utils.ThreadPool;

public class DispatchThreadChain extends Chain {

  @Override
  @SneakyThrows
  public void process(Context context) throws LxitException {
    List<String> urlList = context.getUrlList();
    ThreadPool threadPool = context.getThreadPool();
    int index = 0;
    while (urlList.size() > 0 || threadPool.hasThreadActive()) {
      ResponseHandlerThread thread = threadPool.getThread();
      if (thread != null && index < urlList.size()) {
        String url = urlList.get(index);
        index++;
        thread.setUrl(url);
        thread.setActive(true);
      }
    }
    threadPool.destroy();
  }
}
